package com.example.xd.mvp_project.base;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;

public abstract class BaseActivity extends AppCompatActivity implements BaseView, View.OnClickListener {

    protected MyApplication mMApp;
    protected View mContextView = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundel = getIntent().getExtras();
        initParams(bundel);
        mMApp = (MyApplication) MyApplication.getContext();
        View mView = bindView();
        if (null == mView) {
            mContextView = LayoutInflater.from(this).inflate(bindLayout(), null);
        } else {
            mContextView = mView;
        }
        setContentView(mContextView);
        initView(mContextView);
        setListener();
        doBussiness();
    }

    /**
     * 初始化参数
     *
     * @param bundel
     */
    public abstract void initParams(Bundle bundel);

    /**
     * 绑定视图
     *
     * @return
     */
    public abstract View bindView();

    /**
     * 绑定布局
     *
     * @return
     */
    protected abstract int bindLayout();


    /**
     * 初始化控件
     *
     * @param contextView
     */
    protected abstract void initView(View contextView);

    /**
     * 设置监听
     */
    protected abstract void setListener();


    /**
     * 业务操作
     */
    protected abstract void doBussiness();


    @Override
    public void onClick(View v) {
        widgetClick(v);
    }

    /**
     * view点击
     *
     * @param v
     */
    protected abstract void widgetClick(View v);

    /**
     * 页面跳转
     *
     * @param clz
     */
    public void startActivity(Class<?> clz) {
        startActivity(new Intent(BaseActivity.this, clz));
    }

    /**
     * 携带参数跳转
     *
     * @param clz
     * @param bundle
     */
    public void startActivity(Class<?> clz, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(this, clz);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    @Override
    public void showErr(String err) {

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void showToast(String msg) {

    }

    @Override
    public void hindLoading() {

    }
}
